package com.geekteam.manga.Model;

import java.io.Serializable;

import com.geekteam.manga.provider.MangaContract.Manga;
import com.google.gson.annotations.SerializedName;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;

public class MangaModel implements Parcelable, Serializable{

private static final long serialVersionUID = -617827648917173281L;
	
	public int _id;
	@SerializedName(value="id")
	public String mId;
	@SerializedName(value="title")
	public String mTitle;
	@SerializedName(value="likeNumber")
	public int mLikeNumber;
	@SerializedName(value="viewNumber")
	public int mViewNumber;
	@SerializedName(value="urlCover")
	public String mUrlCover;
	@SerializedName(value="description")
	public String mDescription;
	@SerializedName(value="author")
	public String mAuthor;
	@SerializedName(value="source")
	public String mSource;
	public boolean isFavorite;
	
	public MangaModel(){
		
	}
	
	public MangaModel(Cursor cursor){
		this._id = cursor.getInt(cursor.getColumnIndex(BaseColumns._ID));
		this.mId = cursor.getString(cursor.getColumnIndex(Manga.MANGA_ID));
		this.mTitle = cursor.getString(cursor.getColumnIndex(Manga.MANGA_TITLE));
		this.mLikeNumber = cursor.getInt(cursor.getColumnIndex(Manga.MANGA_LIKENUMBER));
		this.mViewNumber = cursor.getInt(cursor.getColumnIndex(Manga.MANGA_VIEWNUMBER));
		this.mUrlCover = cursor.getString(cursor.getColumnIndex(Manga.MANGA_URLCOVER));
		this.mDescription = cursor.getString(cursor.getColumnIndex(Manga.MANGA_DESCRIPTION));
		this.mAuthor = cursor.getString(cursor.getColumnIndex(Manga.MANGA_AUTHOR));
		this.mSource = cursor.getString(cursor.getColumnIndex(Manga.MANGA_SOURCE));
		this.isFavorite = cursor.getInt(cursor.getColumnIndex(Manga.MANGA_FAVORITE))==1;
	}
	
	public ContentValues contentValues (Long id){
		ContentValues values = new ContentValues();
		values.put(Manga._ID, id);
		values.put(Manga.MANGA_ID, mId);
		values.put(Manga.MANGA_TITLE, mTitle);
		values.put(Manga.MANGA_LIKENUMBER, mLikeNumber);
		values.put(Manga.MANGA_VIEWNUMBER, mViewNumber);
		values.put(Manga.MANGA_URLCOVER, mUrlCover);
		values.put(Manga.MANGA_DESCRIPTION, mDescription);
		values.put(Manga.MANGA_AUTHOR, mAuthor);
		values.put(Manga.MANGA_SOURCE, mSource);
		values.put(Manga.MANGA_FAVORITE, isFavorite);
	
		return values;
		
	}
	
	public ContentValues contentValues(){
		ContentValues values = new ContentValues();
		values.put(Manga.MANGA_ID, mId);
		values.put(Manga.MANGA_TITLE, mTitle);
		values.put(Manga.MANGA_LIKENUMBER, mLikeNumber);
		values.put(Manga.MANGA_VIEWNUMBER, mViewNumber);
		values.put(Manga.MANGA_URLCOVER, mUrlCover);
		values.put(Manga.MANGA_DESCRIPTION, mDescription);
		values.put(Manga.MANGA_AUTHOR, mAuthor);
		values.put(Manga.MANGA_SOURCE, mSource);
		values.put(Manga.MANGA_FAVORITE, isFavorite);
	
		return values;
	}
	
	static public String[] contentColumns() {
		return new String[] { Manga._ID, Manga.MANGA_ID, Manga.MANGA_TITLE, Manga.MANGA_LIKENUMBER,Manga.MANGA_VIEWNUMBER,
				Manga.MANGA_URLCOVER, Manga.MANGA_DESCRIPTION, Manga.MANGA_AUTHOR, Manga.MANGA_SOURCE, Manga.MANGA_FAVORITE};
	}
	
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeInt(_id);
		dest.writeString(mId);
		dest.writeString(mTitle);
		dest.writeInt(mLikeNumber);
		dest.writeInt(mViewNumber);
		dest.writeString(mDescription);
		dest.writeString(mUrlCover);
		dest.writeString(mAuthor);
		dest.writeString(mSource);
		dest.writeInt((isFavorite?1:0));
	}
	
private void readFromParcel(Parcel parcel){
		
		_id = parcel.readInt();
		mId = parcel.readString();
		mTitle = parcel.readString();
		mLikeNumber=parcel.readInt();
		mViewNumber = parcel.readInt();
		mUrlCover = parcel.readString();
		mDescription = parcel.readString();
		mAuthor = parcel.readString();
		mSource = parcel.readString();
		isFavorite = parcel.readInt()==1;

		
	}
	
	public MangaModel(Parcel ptavel){

		readFromParcel(ptavel);
	}
	
	@SuppressWarnings("rawtypes")
	public static final Parcelable.Creator CREATOR = new Parcelable.Creator<MangaModel>(){

		@Override
		public MangaModel createFromParcel(Parcel source) {
			return new MangaModel(source);
		}

		@Override
		public MangaModel[] newArray(int size) {
			return new MangaModel[size];
		}};
		
		public void setmId(String mId){
			this.mId = mId;
		}
		
		public void setmTitle(String mTitle){
			this.mTitle = mTitle;
		}
		
		public String getmId(){
			return mId;
		}
		
		public String getmTitle(){
			return mTitle;
		}
		
		public void setmLikeNumber(int mLikeNumber){
			this.mLikeNumber = mLikeNumber;
		}
		
		public int getmLikeNumber(){
			return mLikeNumber;
		}
		public void setmViewNumber(int mViewNumber){
			this.mViewNumber = mViewNumber;
		}
		
		public int getmViewNumber(){
			return mViewNumber;
		}
		


}
